package com.pv.pulse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.mongodb.BasicDBList;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.pv.pulse.utils.HttpUtil;

public class ProxyPool {
	public static List<ProxyConnection> useProxyList=Collections.synchronizedList(new ArrayList<ProxyConnection>());
	public static HashMap<String, Integer> ipMap=new HashMap<String, Integer>();
	private static int REQUEST_INDEX=0;
	private static int REQUEST_MAX_COUNT=5000;
	public static int thread_count=100;
	public static int request_first_interval=10;
	public static int request_refresh_interval=10;
	private static String get_proxy_url="http://dev.kuaidaili.com/api/getproxy/?orderid=956785625794622&num=100&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&sp1=1&quality=1&sort=1&format=json&sep=1";
	public static String[] request_urls;
	
	public static void initialProxyList(){
		System.out.println("initial start");
		
		JSONObject returnJson=HttpUtil.executeHttpGet(get_proxy_url);
		System.out.println(returnJson);
		
		try {
			JSONObject dataObject=returnJson.getJSONObject("data");
			JSONArray proxyArray=dataObject.getJSONArray("proxy_list");
			for (int j = 0; j < proxyArray.length(); j++) {
				String ipPortStr=proxyArray.getString(j);
				int index=ipPortStr.indexOf(":");
				String ip=ipPortStr.substring(0, index);
				String portStr=ipPortStr.substring(index+1, ipPortStr.length());
				System.out.println("ip:"+ip+"port:"+portStr);
				int port=Integer.parseInt(portStr);
				useProxyList.add(new ProxyConnection(ip, port));
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("initial finished");
	}
	
	public static void init(){
		try {
			Properties prop = new Properties();//属性集合对象
			File file=new File("a.txt");
			file.createNewFile();
			InputStream in=ProxyPool.class.getClassLoader().getResourceAsStream("/system.properties");
//			FileInputStream fis = new FileInputStream("classpath:system.properties");//属性文件流
			prop.load(in);//将属性文件流装载到Properties对象中   
			REQUEST_MAX_COUNT=Integer.parseInt(prop.getProperty("request_max_count"));
			thread_count=Integer.parseInt(prop.getProperty("thread.count"));
			get_proxy_url=prop.getProperty("get_proxy_request_url");
			request_first_interval=Integer.parseInt(prop.getProperty("request_first_interval"));
			request_refresh_interval=Integer.parseInt(prop.getProperty("request_refresh_interval"));
			String requestUrlStr=prop.getProperty("request.url");
			request_urls=requestUrlStr.split(",");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initialProxyList();
	}
	
	static{
		init();
		
//		try {
			
			
//			Mongo mongo = new Mongo("120.24.75.6", 27017);
//			DB db=mongo.getDB("proxyIP");
//			DBCollection collection=db.getCollection("proxyIP_collection");
//			System.out.println("ProxyIP记录数："+collection.count());
//			List<DBObject> list = collection.find().toArray();
//	        if(list != null&& list.size() > 0){
//	        	for(int i=0;i<list.size();i++){
//	        		BasicDBList proxyIPDBList= (BasicDBList) list.get(i).get("proxy_ip");
//	        		String ip=(String)proxyIPDBList.get(0);
//		        	System.out.println(proxyIPDBList.get(0));
//		        	BasicDBList proxyPortDBList= (BasicDBList) list.get(i).get("proxy_port");
//		        	int port=Integer.parseInt(proxyPortDBList.get(0).toString());
//		        	System.out.println(proxyPortDBList.get(0));
//		        	useProxyList.add(new ProxyConnection(ip, port));
//	        	}
//	        }
	        
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		ProxyConnection conn1=new ProxyConnection("119.135.185.99", 9797);
//		useProxyList.add(conn1);
//		ProxyConnection conn2=new ProxyConnection("117.169.4.139", 80);
//		useProxyList.add(conn2);
//		ProxyConnection conn3=new ProxyConnection("111.11.80.157", 80);
//		useProxyList.add(conn3);
//		ProxyConnection conn4=new ProxyConnection("101.66.253.22", 8080);
//		useProxyList.add(conn4);
//		ProxyConnection conn5=new ProxyConnection("221.224.163.28", 80);
//		useProxyList.add(conn5);
//		ProxyConnection conn6=new ProxyConnection("117.169.4.156", 80);
//		useProxyList.add(conn6);
//		ProxyConnection conn7=new ProxyConnection("117.169.4.132", 80);
//		useProxyList.add(conn7);
//		ProxyConnection conn8=new ProxyConnection("118.180.15.151", 8102);
//		useProxyList.add(conn8);
//		ProxyConnection conn9=new ProxyConnection("183.91.33.76", 8087);
//		useProxyList.add(conn9);
//		ProxyConnection conn10=new ProxyConnection("118.180.15.152", 8102);
//		useProxyList.add(conn10);
//		
//		useProxyList.add(new ProxyConnection("111.11.80.159", 80));
//		useProxyList.add(new ProxyConnection("110.87.226.92", 8998));
//		useProxyList.add(new ProxyConnection("121.192.226.103", 8998));
//		useProxyList.add(new ProxyConnection("59.61.217.230", 8998));
//		useProxyList.add(new ProxyConnection("180.124.25.6", 8998));
//		useProxyList.add(new ProxyConnection("125.124.141.89", 3128));
//		useProxyList.add(new ProxyConnection("182.148.116.148", 8998));
//		useProxyList.add(new ProxyConnection("60.23.249.43", 80));
//		useProxyList.add(new ProxyConnection("183.13.180.142", 8118));
//		useProxyList.add(new ProxyConnection("122.245.30.110", 8998));
//		useProxyList.add(new ProxyConnection("183.224.99.149", 8123));
//		useProxyList.add(new ProxyConnection("202.203.158.200", 8998));
//		
//		
//		useProxyList.add(new ProxyConnection("202.203.158.200", 8998));
//		useProxyList.add(new ProxyConnection("183.239.225.44", 8888));
//		useProxyList.add(new ProxyConnection("210.5.149.43", 8090));
//		useProxyList.add(new ProxyConnection("182.148.117.59", 8998));
//		useProxyList.add(new ProxyConnection("58.42.185.16", 8998));
//		useProxyList.add(new ProxyConnection("115.148.249.141", 8998));
//		useProxyList.add(new ProxyConnection("182.148.116.148", 8998));
//		useProxyList.add(new ProxyConnection("49.89.71.81", 8998));
//		useProxyList.add(new ProxyConnection("59.57.72.2", 8998));
//		useProxyList.add(new ProxyConnection("202.203.158.200", 8998));
//		useProxyList.add(new ProxyConnection("171.223.23.107", 8998));
//		useProxyList.add(new ProxyConnection("171.113.108.195", 8998));
//		
//		useProxyList.add(new ProxyConnection("114.96.173.40", 8998));
//		useProxyList.add(new ProxyConnection("171.38.22.181", 8123));
//		useProxyList.add(new ProxyConnection("125.110.40.210", 8998));
//		useProxyList.add(new ProxyConnection("113.12.167.45", 8998));
//		useProxyList.add(new ProxyConnection("182.247.100.15", 8998));
//		useProxyList.add(new ProxyConnection("220.185.197.231", 8998));
//		useProxyList.add(new ProxyConnection("117.93.192.81", 8998));
//		useProxyList.add(new ProxyConnection("119.132.158.248", 8998));
//		useProxyList.add(new ProxyConnection("222.91.216.162", 8998));
//		useProxyList.add(new ProxyConnection("27.18.184.215", 8998));
//		useProxyList.add(new ProxyConnection("116.16.162.105", 8998));
//		useProxyList.add(new ProxyConnection("111.120.80.137", 8998));
	}
	
	public synchronized static ProxyConnection getProxy(){
		for(int i=0;i<useProxyList.size();i++){
			ProxyConnection pc=useProxyList.get(i);
			if(pc.getStatus().equals(ProxyConnection.PROXY_STATUS_CLOSE)){
				if(pc.getErrorCount()<1){
					pc.setStatus(ProxyConnection.PROXY_STATUS_OPEN);
					useProxyList.remove(pc);
					useProxyList.add(pc);
					System.out.println("异常IP数量："+ipMap.keySet().size()+",正常IP数量："+useProxyList.size());
					return pc;
				}
				useProxyList.remove(pc);
				i--;
				ipMap.put(pc.getProxyConfig().getProxyHost(), pc.getErrorCount());
				System.out.println("异常IP数量："+ipMap.keySet().size()+",正常IP数量："+useProxyList.size());
			}
		}
		initialProxyList();
		for(int i=0;i<useProxyList.size();i++){
			ProxyConnection pc=useProxyList.get(i);
			if(pc.getStatus().equals(ProxyConnection.PROXY_STATUS_CLOSE)){
				if(pc.getErrorCount()<1){
					pc.setStatus(ProxyConnection.PROXY_STATUS_OPEN);
					useProxyList.remove(pc);
					useProxyList.add(pc);
					System.out.println("异常IP数量："+ipMap.keySet().size()+",正常IP数量："+useProxyList.size());
					return pc;
				}
				useProxyList.remove(pc);
				i--;
				ipMap.put(pc.getProxyConfig().getProxyHost(), pc.getErrorCount());
				System.out.println("异常IP数量："+ipMap.keySet().size()+",正常IP数量："+useProxyList.size());
			}
		}
		return null;
	}
	
	public synchronized static boolean requestIndexPlus(){
		if(REQUEST_INDEX<REQUEST_MAX_COUNT){
			REQUEST_INDEX++;
			System.out.println("Request count: "+REQUEST_INDEX);
			return true;
		}
		return false;
	}
	public synchronized static boolean skipThread(){
		if(REQUEST_INDEX>=REQUEST_MAX_COUNT){
			return true;
		}
		return false;
	}
}
